home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perllol.z / perllol
Text File  |  1998-10-30  |  13KB  |  463 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlLoL - Manipulating Lists of Lists in Perl
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12. DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnn aaaannnndddd AAAAcccccccceeeessssssss ooooffff LLLLiiiissssttttssss ooooffff LLLLiiiissssttttssss
  13.      The simplest thing to build is a list of lists (sometimes called an array
  14.      of arrays).  It's reasonably easy to understand, and almost everything
  15.      that applies here will also be applicable later on with the fancier data
  16.      structures.
  17.  
  18.      A list of lists, or an array of an array if you would, is just a regular
  19.      old array @LoL that you can get at with two subscripts, like $LoL[3][2].
  20.      Here's a declaration of the array:
  21.  
  22.          # assign to our array a list of list references
  23.          @LoL = (
  24.                 [ "fred", "barney" ],
  25.                 [ "george", "jane", "elroy" ],
  26.                 [ "homer", "marge", "bart" ],
  27.          );
  28.  
  29.          print $LoL[2][2];
  30.        bart
  31.  
  32.      Now you should be very careful that the outer bracket type is a round
  33.      one, that is, parentheses.  That's because you're assigning to an @list,
  34.      so you need parentheses.  If you wanted there _n_o_t to be an @LoL, but
  35.      rather just a reference to it, you could do something more like this:
  36.  
  37.          # assign a reference to list of list references
  38.          $ref_to_LoL = [
  39.              [ "fred", "barney", "pebbles", "bambam", "dino", ],
  40.              [ "homer", "bart", "marge", "maggie", ],
  41.              [ "george", "jane", "alroy", "judy", ],
  42.          ];
  43.  
  44.          print $ref_to_LoL->[2][2];
  45.  
  46.      Notice that the outer bracket type has changed, and so our access syntax
  47.      has also changed.  That's because unlike C, in perl you can't freely
  48.      interchange arrays and references thereto.  $ref_to_LoL is a reference to
  49.      an array, whereas @LoL is an array proper.  Likewise, $LoL[2] is not an
  50.      array, but an array ref.  So how come you can write these:
  51.  
  52.          $LoL[2][2]
  53.          $ref_to_LoL->[2][2]
  54.  
  55.      instead of having to write these:
  56.  
  57.          $LoL[2]->[2]
  58.          $ref_to_LoL->[2]->[2]
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  71.  
  72.  
  73.  
  74.      Well, that's because the rule is that on adjacent brackets only (whether
  75.      square or curly), you are free to omit the pointer dereferencing arrow.
  76.      But you cannot do so for the very first one if it's a scalar containing a
  77.      reference, which means that $ref_to_LoL always needs it.
  78.  
  79. GGGGrrrroooowwwwiiiinnnngggg YYYYoooouuuurrrr OOOOwwwwnnnn
  80.      That's all well and good for declaration of a fixed data structure, but
  81.      what if you wanted to add new elements on the fly, or build it up
  82.      entirely from scratch?
  83.  
  84.      First, let's look at reading it in from a file.  This is something like
  85.      adding a row at a time.  We'll assume that there's a flat file in which
  86.      each line is a row and each word an element.  If you're trying to develop
  87.      an @LoL list containing all these, here's the right way to do that:
  88.  
  89.          while (<>) {
  90.              @tmp = split;
  91.              push @LoL, [ @tmp ];
  92.          }
  93.  
  94.      You might also have loaded that from a function:
  95.  
  96.          for $i ( 1 .. 10 ) {
  97.              $LoL[$i] = [ somefunc($i) ];
  98.          }
  99.  
  100.      Or you might have had a temporary variable sitting around with the list
  101.      in it.
  102.  
  103.          for $i ( 1 .. 10 ) {
  104.              @tmp = somefunc($i);
  105.              $LoL[$i] = [ @tmp ];
  106.          }
  107.  
  108.      It's very important that you make sure to use the [] list reference
  109.      constructor.  That's because this will be very wrong:
  110.  
  111.          $LoL[$i] = @tmp;
  112.  
  113.      You see, assigning a named list like that to a scalar just counts the
  114.      number of elements in @tmp, which probably isn't what you want.
  115.  
  116.      If you are running under use strict, you'll have to add some declarations
  117.      to make it happy:
  118.  
  119.          use strict;
  120.          my(@LoL, @tmp);
  121.          while (<>) {
  122.              @tmp = split;
  123.              push @LoL, [ @tmp ];
  124.          }
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  137.  
  138.  
  139.  
  140.      Of course, you don't need the temporary array to have a name at all:
  141.  
  142.          while (<>) {
  143.              push @LoL, [ split ];
  144.          }
  145.  
  146.      You also don't have to use _p_u_s_h().  You could just make a direct
  147.      assignment if you knew where you wanted to put it:
  148.  
  149.          my (@LoL, $i, $line);
  150.          for $i ( 0 .. 10 ) {
  151.              $line = <>;
  152.              $LoL[$i] = [ split ' ', $line ];
  153.          }
  154.  
  155.      or even just
  156.  
  157.          my (@LoL, $i);
  158.          for $i ( 0 .. 10 ) {
  159.              $LoL[$i] = [ split ' ', <> ];
  160.          }
  161.  
  162.      You should in general be leery of using potential list functions in a
  163.      scalar context without explicitly stating such.  This would be clearer to
  164.      the casual reader:
  165.  
  166.          my (@LoL, $i);
  167.          for $i ( 0 .. 10 ) {
  168.              $LoL[$i] = [ split ' ', scalar(<>) ];
  169.          }
  170.  
  171.      If you wanted to have a $ref_to_LoL variable as a reference to an array,
  172.      you'd have to do something like this:
  173.  
  174.          while (<>) {
  175.              push @$ref_to_LoL, [ split ];
  176.          }
  177.  
  178.      Actually, if you were using strict, you'd have to declare not only
  179.      $ref_to_LoL as you had to declare @LoL, but you'd _a_l_s_o having to
  180.      initialize it to a reference to an empty list.  (This was a bug in perl
  181.      version 5.001m that's been fixed for the 5.002 release.)
  182.  
  183.          my $ref_to_LoL = [];
  184.          while (<>) {
  185.              push @$ref_to_LoL, [ split ];
  186.          }
  187.  
  188.      Ok, now you can add new rows.  What about adding new columns?  If you're
  189.      dealing with just matrices, it's often easiest to use simple assignment:
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  203.  
  204.  
  205.  
  206.          for $x (1 .. 10) {
  207.              for $y (1 .. 10) {
  208.                  $LoL[$x][$y] = func($x, $y);
  209.              }
  210.          }
  211.  
  212.          for $x ( 3, 7, 9 ) {
  213.              $LoL[$x][20] += func2($x);
  214.          }
  215.  
  216.      It doesn't matter whether those elements are already there or not: it'll
  217.      gladly create them for you, setting intervening elements to undef as need
  218.      be.
  219.  
  220.      If you wanted just to append to a row, you'd have to do something a bit
  221.      funnier looking:
  222.  
  223.          # add new columns to an existing row
  224.          push @{ $LoL[0] }, "wilma", "betty";
  225.  
  226.      Notice that I _c_o_u_l_d_n'_t say just:
  227.  
  228.          push $LoL[0], "wilma", "betty";  # WRONG!
  229.  
  230.      In fact, that wouldn't even compile.  How come?  Because the argument to
  231.      _p_u_s_h() must be a real array, not just a reference to such.
  232.  
  233. AAAAcccccccceeeessssssss aaaannnndddd PPPPrrrriiiinnnnttttiiiinnnngggg
  234.      Now it's time to print your data structure out.  How are you going to do
  235.      that?  Well, if you want only one of the elements, it's trivial:
  236.  
  237.          print $LoL[0][0];
  238.  
  239.      If you want to print the whole thing, though, you can't say
  240.  
  241.          print @LoL;         # WRONG
  242.  
  243.      because you'll get just references listed, and perl will never
  244.      automatically dereference things for you.  Instead, you have to roll
  245.      yourself a loop or two.  This prints the whole structure, using the
  246.      shell-style _f_o_r() construct to loop across the outer set of subscripts.
  247.  
  248.          for $aref ( @LoL ) {
  249.              print "\t [ @$aref ],\n";
  250.          }
  251.  
  252.      If you wanted to keep track of subscripts, you might do this:
  253.  
  254.          for $i ( 0 .. $#LoL ) {
  255.              print "\t elt $i is [ @{$LoL[$i]} ],\n";
  256.          }
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  269.  
  270.  
  271.  
  272.      or maybe even this.  Notice the inner loop.
  273.  
  274.          for $i ( 0 .. $#LoL ) {
  275.              for $j ( 0 .. $#{$LoL[$i]} ) {
  276.                  print "elt $i $j is $LoL[$i][$j]\n";
  277.              }
  278.          }
  279.  
  280.      As you can see, it's getting a bit complicated.  That's why sometimes is
  281.      easier to take a temporary on your way through:
  282.  
  283.          for $i ( 0 .. $#LoL ) {
  284.              $aref = $LoL[$i];
  285.              for $j ( 0 .. $#{$aref} ) {
  286.                  print "elt $i $j is $LoL[$i][$j]\n";
  287.              }
  288.          }
  289.  
  290.      Hmm... that's still a bit ugly.  How about this:
  291.  
  292.          for $i ( 0 .. $#LoL ) {
  293.              $aref = $LoL[$i];
  294.              $n = @$aref - 1;
  295.              for $j ( 0 .. $n ) {
  296.                  print "elt $i $j is $LoL[$i][$j]\n";
  297.              }
  298.          }
  299.  
  300.  
  301. SSSSlllliiiicccceeeessss
  302.      If you want to get at a slice (part of a row) in a multidimensional
  303.      array, you're going to have to do some fancy subscripting.  That's
  304.      because while we have a nice synonym for single elements via the pointer
  305.      arrow for dereferencing, no such convenience exists for slices.
  306.      (Remember, of course, that you can always write a loop to do a slice
  307.      operation.)
  308.  
  309.      Here's how to do one operation using a loop.  We'll assume an @LoL
  310.      variable as before.
  311.  
  312.          @part = ();
  313.          $x = 4;
  314.          for ($y = 7; $y < 13; $y++) {
  315.              push @part, $LoL[$x][$y];
  316.          }
  317.  
  318.      That same loop could be replaced with a slice operation:
  319.  
  320.          @part = @{ $LoL[4] } [ 7..12 ];
  321.  
  322.      but as you might well imagine, this is pretty rough on the reader.
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  335.  
  336.  
  337.  
  338.      Ah, but what if you wanted a _t_w_o-_d_i_m_e_n_s_i_o_n_a_l _s_l_i_c_e, such as having $x run
  339.      from 4..8 and $y run from 7 to 12?  Hmm... here's the simple way:
  340.  
  341.          @newLoL = ();
  342.          for ($startx = $x = 4; $x <= 8; $x++) {
  343.              for ($starty = $y = 7; $y <= 12; $y++) {
  344.                  $newLoL[$x - $startx][$y - $starty] = $LoL[$x][$y];
  345.              }
  346.          }
  347.  
  348.      We can reduce some of the looping through slices
  349.  
  350.          for ($x = 4; $x <= 8; $x++) {
  351.              push @newLoL, [ @{ $LoL[$x] } [ 7..12 ] ];
  352.          }
  353.  
  354.      If you were into Schwartzian Transforms, you would probably have selected
  355.      map for that
  356.  
  357.          @newLoL = map { [ @{ $LoL[$_] } [ 7..12 ] ] } 4 .. 8;
  358.  
  359.      Although if your manager accused of seeking job security (or rapid
  360.      insecurity) through inscrutable code, it would be hard to argue. :-) If I
  361.      were you, I'd put that in a function:
  362.  
  363.          @newLoL = splice_2D( \@LoL, 4 => 8, 7 => 12 );
  364.          sub splice_2D {
  365.              my $lrr = shift;        # ref to list of list refs!
  366.              my ($x_lo, $x_hi,
  367.                  $y_lo, $y_hi) = @_;
  368.  
  369.              return map {
  370.                  [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ]
  371.              } $x_lo .. $x_hi;
  372.          }
  373.  
  374.  
  375. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  376.      _p_e_r_l_d_a_t_a(1), _p_e_r_l_r_e_f(1), _p_e_r_l_d_s_c(1)
  377.  
  378. AAAAUUUUTTTTHHHHOOOORRRR
  379.      Tom Christiansen <_t_c_h_r_i_s_t@_p_e_r_l._c_o_m>
  380.  
  381.      Last udpate: Sat Oct  7 19:35:26 MDT 1995
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))                                                          PPPPEEEERRRRLLLLLLLLOOOOLLLL((((1111))))
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.                                                                         PPPPaaaaggggeeee 7777
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.